1.请求命令
请求命令就是以继承类的方式来代替前面的注解方式。
接着上文,首先定义一个helloCommand:
public class HelloCommand extends HystrixCommand<String> {
RestTemplate restTemplate;
public HelloCommand(Setter setter,RestTemplate restTemplate) { super(setter); this.restTemplate = restTemplate; }
@Override protected String run() throws Exception { return restTemplate.getForObject("http://provider/hello",String.class); } }
|
调用方法:
@GetMapping("/hello2") public void hello2(){ HelloCommand helloCommand = new HelloCommand(HystrixCommand.Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("hellojava")), restTemplate);
String execute = helloCommand.execute(); System.out.println(execute); HelloCommand helloCommand2 = new HelloCommand(HystrixCommand.Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("hellojava")), restTemplate);
String s = null; try { Future<String> queue = helloCommand2.queue(); s = queue.get(); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } System.out.println(s);
}
|
启动eureka server和provider以及hystrix ,访问http://localhost:3000/hello2 在控制台查看结果。
1.1通过注解实现请求异步调用
@HystrixCommand(fallbackMethod = "error") public Future<String> hello2(){ return new AsyncResult<String>(){
@Override public String invoke() { return restTemplate.getForObject("http://provider/hello",String.class); } }; }
|
@GetMapping("/hello3") public void hello3(){ Future<String> stringFuture = helloService.hello2(); String s = null; try { s = stringFuture.get(); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } System.out.println(s);
}
|
重启hystrix项目,访问:http://localhost:3000/hello3 如下:

1.2通过继承方式使用Hystrix, 重写继承类的 getFallback 方法实现服务容错/降级。
public class HelloCommand extends HystrixCommand<String> {
RestTemplate restTemplate;
public HelloCommand(Setter setter,RestTemplate restTemplate) { super(setter); this.restTemplate = restTemplate; }
@Override protected String run() throws Exception { return restTemplate.getForObject("http://provider/hello",String.class); }
@Override protected String getFallback(){ return "error-extends"; }
}
|
重启hystrix项目 访问hello2接口。(启动2个provider,都注册到eureka,,再关闭一个,刷新才能看到效果,与一开始的注解式方式相似)
2.异常处理
就是当发起服务时,如果不是provider的原因导致请求调用失败,而是 consumer 中本身代码有问题导致的请求失败,即 consumer中抛出了异常,这个时候,也会自动进行服务降级,只不过这个时候降级,我们还需要知道到底哪里出错了。
如下例实例代码,如果hello方法执行时抛出异常,那么一样会进行服务降级,进入到 error 方法中,然后获取到异常的详细及信息。
@Service public class HelloService {
@Autowired RestTemplate restTemplate;
@HystrixCommand(fallbackMethod = "error") public String hello(){ int i = 1 / 0; return restTemplate.getForObject("http://provider/hello",String.class); }
public String error(Throwable t){ return "error"+t.getMessage(); } }
|
这是注解的方式,也可以通过继承的方式:
public class HelloCommand extends HystrixCommand<String> {
RestTemplate restTemplate;
public HelloCommand(Setter setter,RestTemplate restTemplate) { super(setter); this.restTemplate = restTemplate; }
@Override protected String run() throws Exception { int i = 1 / 0; return restTemplate.getForObject("http://provider/hello",String.class); }
@Override protected String getFallback(){ return "error-extends"+getFailedExecutionException().getMessage(); }
}
|
如果是通过继承的方式来做 Hystrix ,在getFallback 方法中,我们可以通过 getExecutionException 方法来获取执行的异常信息。
另一种可能性。如果抛出异常了,我们希望异常直接抛出,不要服务降级,那么只需要配置忽略某一个异常即可:
@Override protected String hello() { int i = 1 / 0; return restTemplate.getForObject("http://provider/hello",String.class); }
|
代码示例地址:https://github.com/astronger/springcloud-simple-samples